package net.hn.hnms.biz.cockpit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.cockpit.domain.MonthThreeViolations;
import net.hn.hnms.biz.cockpit.domain.vo.OptionVo;
import net.hn.hnms.biz.cockpit.domain.vo.ThreeViolationsVo;
import net.hn.hnms.biz.cockpit.mapper.MonthThreeViolationsMapper;
import net.hn.hnms.biz.cockpit.service.IMonthThreeViolationsService;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MonthThreeViolationsServiceImpl extends ServiceImpl<MonthThreeViolationsMapper, MonthThreeViolations> implements IMonthThreeViolationsService {

    @Override
    public ThreeViolationsVo getThreeViolations(String mineCode) {
        ThreeViolationsVo threeViolationsVo = new ThreeViolationsVo();
        String month = DateUtils.dateTimeNow(DateUtils.YYYY_MM);
        List<MonthThreeViolations> monthThreeViolations = baseMapper.selectList(new LambdaQueryWrapper<MonthThreeViolations>().eq(MonthThreeViolations::getMineCode, mineCode)
                .eq(MonthThreeViolations::getCheckPeriod, month));
        if (CollectionUtils.isNotEmpty(monthThreeViolations)) {
            List<OptionVo> result = monthThreeViolations.stream().map(monthThreeViolation -> new OptionVo(monthThreeViolation.getType(), monthThreeViolation.getViolationTotal())).collect(Collectors.toList());
            int sum = result.stream().mapToInt(u -> Integer.parseInt(u.getValue())).sum();
            threeViolationsVo.setList(result).setTotal(sum);
        }
        return threeViolationsVo;
    }

}
